﻿/**
 * 时间日期初始化
 */
function dateTimeInit(){
	
    //$('dateLabel').innerText = new Date().format("yyyy.MM.dd");
    var d = new Date();
    var h = d.getHours();
    var i = d.getMinutes().toString();
	i = i.length<2?'0'+i:i;
    var m = h > 12 ? "PM" : "AM";
    var timeFormat =new Date().format("yyyy.MM.dd")+"  "+ m + " " + h + ":" + i;
    document.getElementById('dateLabel').innerText = timeFormat;
    setInterval(function(){
        var d = new Date();
        var h = d.getHours();
        var i = d.getMinutes().toString();
		i = i.length<2?'0'+i:i;
        var m = h > 12 ? "PM" : "AM";
        var timeFormat =new Date().format("yyyy.MM.dd")+"  "+ m + " " + h + ":" + i;
        document.getElementById('dateLabel').innerText = timeFormat;
    }, 1000);
	var week;
	switch (d.getDay()){
	  case 1:
	   week="星期一";
	   break;
	  case 2:
	   week="星期二";
	   break;
	  case 3:
	   week="星期三";
	   break;
	  case 4:
	   week="星期四";
	   break;
	  case 5:
	   week="星期五";
	   break;
	  case 6:
	   week="星期六";
	   break;
	  case 0:
	   week="星期日";
	   break;
	 }
	 document.getElementById('weekLabel').innerHTML = week;
	 get_degrees();
}

var now_date = ''
var city_obj = null;
var net_obj_time = null;
function get_degrees(){
    var city_address = 'http://ip.cms.sohu.com/ip.do?type=json';
    var weatherData = getCookie('weather');
    console.log('---------cookie,weatherData:' + weatherData + '-----------');
    if (weatherData) {
        console.log('--------get cookie---------');
        var content = argument2;
        var weather_low = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"Low\"\:\"[\S\s]*?\"\,/)[0].replace(/\"Low\"\:\"|\"\,/g, '');
        var weather_high = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"High\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"High\"\:\"|\"\}\,/g, '');
        var weather = weather_low + '/' + weather_high + "℃";
        var weather_img = content.match(/\"Weather\"\:\{\"Desc\"[\S\s]*?\"\}\,/)[0].match(/\"Symbol\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"Symbol\"\:\"|\"\}\,/g, '');
        /*var weather_logo = "http://news.sohu.com/upload/weather1217/wp" + weather_img[0] + weather_img[1] + ".gif";
        var str_degrees = '<div class="degrees fl">' + weather + '</div><img src="' + weather_logo + '" class="fl">';
        document.getElementById("degrees").innerHTML = str_degrees;*/
		var weather_logo = "http://news.sohu.com/upload/weather1217/wp" + weather_img[0] + weather_img[1] + ".gif";
            //var str_degrees = '<div class="degrees fl">' + weather + '</div><img  height="32" width="32" src="' + weather_logo + '" class="fl">';
		var str_degrees = '<img   src="' + weather_logo + '">';
		document.getElementById("timeLabel").innerHTML='今日气温：'+weather;
		document.getElementById("degrees").innerHTML = str_degrees;

    }
    else {
		
        city_obj.getWithAsync(city_address, 30);
        city_obj.oncomplete = function(e){
            var argument0 = e.arguments[0].toLocaleString(); //URL
            var argument1 = e.arguments[1].toLocaleString(); //Response Code
            var argument2 = e.arguments[2].toLocaleString(); //content
            var content = argument2;
            var address = content.match(/cid\:[\S\s]*?\,/)[0].replace(/cid\:|\,/g, '');
            
            var date = new Date();
            var YY = date.getYear();
            if (YY < 1000) {
                YY += 1900;
            }
			YY = YY.toString();
            var MM = (date.getMonth() + 1).toString();
			MM = MM.length==1?'0'+MM:MM;
            var DD = date.getDate().toString();
			DD = DD.length<2?'0'+DD:DD;
            now_date = YY + MM + DD;
            var degrees = "http://m.sohu.com/weather/cms/" + now_date + "/" + address + ".3days.json";
            console.log('------------degrees:' + degrees + '------------');
            net_obj_time.getWithAsync(degrees, 30);
        }
        net_obj_time.oncomplete = function(e){
            var argument0 = e.arguments[0].toLocaleString(); //URL
            var argument1 = e.arguments[1].toLocaleString(); //Response Code
            var argument2 = e.arguments[2].toLocaleString(); //content
            var content = argument2;
            console.log('-----------net_obj_time content:' + content + '-------------');
            var weather_low = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"Low\"\:\"[\S\s]*?\"\,/)[0].replace(/\"Low\"\:\"|\"\,/g, '');
            var weather_high = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"High\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"High\"\:\"|\"\}\,/g, '');
            var weather = weather_low + '/' + weather_high + "℃";
            var weather_img = content.match(/\"Weather\"\:\{\"Desc\"[\S\s]*?\"\}\,/)[0].match(/\"Symbol\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"Symbol\"\:\"|\"\}\,/g, '');
            var weather_logo = "http://news.sohu.com/upload/weather1217/wp" + weather_img[0] + weather_img[1] + ".gif";
            //var str_degrees = '<div class="degrees fl">' + weather + '</div><img  height="32" width="32" src="' + weather_logo + '" class="fl">';
			var str_degrees = '<img   src="' + weather_logo + '">';
			document.getElementById("timeLabel").innerHTML='今日气温：'+weather;
            document.getElementById("degrees").innerHTML = str_degrees;
            setCookie('weather', content, 1)
        }
    }
    
    
}

function setCookie(c_name, value, expiredays){
	try {
		var exdate = new Date()
		exdate.setDate(exdate.getDate() + expiredays)
		document.cookie = c_name + "=" + escape(value) +
		((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
		
	}catch(e){
		console.log('----------in setCookies error:'+e.message+'---------');
	}
}

function getCookie(c_name){
	try {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(c_name + "=")
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1
				c_end = document.cookie.indexOf(";", c_start)
				if (c_end == -1) 
					c_end = document.cookie.length
				return unescape(document.cookie.substring(c_start, c_end))
			}
		}
	} 
	catch (e) {
		console.log('----------in getCookies error:' + e.message + '---------');
	}
    
    return ""
}

Date.prototype.format = function(format) //author: meizz 
{ 
  var o = { 
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(),    //day 
    "h+" : this.getHours(),   //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter 
    "S" : this.getMilliseconds() //millisecond 
  } 
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
    (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o)if(new RegExp("("+ k +")").test(format)) 
    format = format.replace(RegExp.$1, 
      RegExp.$1.length==1 ? o[k] : 
        ("00"+ o[k]).substr((""+ o[k]).length)); 
  return format; 
}

/*function $(){
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) //查找作为参数提供的所有元素
    {
        var element = arguments[i];
        if (typeof element == 'string') {
            element = document.getElementById(element);
        }
        if (arguments.length == 1) {
            return element;
        }
        elements.push(element);
    }
    return elements;
}*/


